package com.wave.traffic.modbus;

import cn.hutool.core.util.NumberUtil;
import com.wave.traffic.util.SocketClientUtil;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Client2 {
    public static void main(String[] args) {
            try {
                System.out.println("尝试连接");
                Socket s = new Socket("192.168.5.239", 502);
                System.out.println("连接成功！");
                //构建IO
                InputStream is = s.getInputStream();
                OutputStream os = s.getOutputStream();

//                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
                BufferedOutputStream bw = new BufferedOutputStream( os );
                //向服务器端发送一条消息
                String input = "000100000006FF0100640004";
                //获取多条信息
                String inputList = "000100000008FF0F006400040103";
                //写单个DO状态(0x05)
                String dangeDO = "000100000006FF050064FF00";
                String DOduqu = "0001 0000 0006 FF05 0064 0004";
                //将DO1断开，
                String dangeduankaiDO = "000100000006FF0500640000";

                /** DI读取 **/
                String DI1 = "000100000006FF0200C80004";

                /** DI滤波读取 **/
                String DIlvbo = "000100000006FF03012C0004";

                /** 写单个DI滤波参数 **/
                String DIlvcoIn = "000100000006FF06012C0014";


                /***
                 *  00 01 00 00 00 06 FF 05 00 66 FF 00
                 *
                 *  00 01 00 00 00 06 FF 05 00 66 00 00
                 *
                 *  00 00 00 00 00 04 01 02 01 01
                 *
                 *  00 00 00 00 00 04 01 02 01 00
                 *
                 *  00 00 00 00 00 04 01 02 01 02
                 *
                 *  0x00,0x01,0x00,0x00,0x00,0x06,0xff,0x05,0x00,0x64,0x00,0x00
                 *  0x00,0x01,0x00,0x00,0x00,0x06,0xff,0x05,0x00,0x64,0x00,0x00
                 */
                //将DO3 开
                String zhajiDO3you = "000100000006FF0500660000";
                System.out.println(zhajiDO3you);
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //for (int i = 0; i < 20; i++) {
                    byte[] byteInput = hexStr2bytes( "000100000006FF050064FF00" );
                  //  byte[] byteInput = hexStr2bytes( "000100000006FF0200C80004" );
                    System.out.println("hex string "+ bytesToHexString(byteInput));
                    bw.write( byteInput );
                //}
//                byte[] byteInput = hexStr2bytes( zhajiDO3you );
//                System.out.println("hex string "+ bytesToHexString(byteInput));

                bw.flush();

                //读取服务器返回的消息
//                BufferedReader br = new BufferedReader(new InputStreamReader(is));
//                String mess = br.readLine();

                BufferedInputStream bis = new BufferedInputStream( is );
                byte[] bytes = new byte[10]; // 一次读取一个byte
                String ret = "";
                bis.read(bytes);
                ret += bytesToHexString(bytes) + " ";  //调用字节转化16进制字符串方法


                System.out.println("服务器："+ret);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    public static byte[] hexStr2bytes(String hexStr) {

        if(hexStr.length()%2 != 0) {//长度为单数
            hexStr = "0" + hexStr;//前面补0
        }
        char[] chars = hexStr.toCharArray();
        int len = chars.length/2;
        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++) {
            int x = i*2;
            bytes[i] = (byte)Integer.parseInt(String.valueOf(new char[]{chars[x], chars[x+1]}), 16);
        }
        return bytes;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            stringBuilder.append("0x");

            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            if (i != src.length-1) {
                stringBuilder.append(",");

            }
        }
        return stringBuilder.toString();
    }

    public static int getLastNum(String numStr){
        numStr = numStr.trim();
        System.out.println("str:" + numStr);
        String[] strs = numStr.split(",");
        String lastPart = strs[strs.length-1];
        System.out.println("lastPart:" + lastPart);
        int lastPartInt = NumberUtil.parseInt(lastPart);
        System.out.println("lastPartInt:" + lastPartInt);
        return lastPartInt;
    }
}